package com.hp.team.bookstore.model.profile;

import java.util.Calendar;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "USER")
public class User extends Profile implements Comparable<User>{

	@Embedded
	@AttributeOverrides(value = { @AttributeOverride(name = "buildingNumber", column = @Column(name = "BUSINESS_ADDRESS_BUILDING_NUMBER")),
			@AttributeOverride(name = "street", column = @Column(name = "BUSINESS_ADDRESS_STREET")),
			@AttributeOverride(name = "state", column = @Column(name = "BUSINESS_ADDRESS_STATE")),
			@AttributeOverride(name = "zipCode", column = @Column(name = "BUSINESS_ADDRESS_ZIP_CODE")),
			@AttributeOverride(name = "city", column = @Column(name = "BUSINESS_ADDRESS_CITY")),
			@AttributeOverride(name = "country", column = @Column(name = "BUSINESS_ADDRESS_COUNTRY")) })
	private Address businessAddress;

	@Embedded
	@AttributeOverrides(value = { @AttributeOverride(name = "countryCode", column = @Column(name = "BUSINESS_TELEPHONE_COUNTRY_CODE")),
			@AttributeOverride(name = "number", column = @Column(name = "BUSINESS_TELEPHONE_NUMBER")) })
	private Telephone businessTelephone;

	@Temporal(TemporalType.DATE)
	@Column(name = "HIRING_DATE")
	private Calendar hiringDate;

	@Column(name = "USERNAME", nullable = false)
	private String username;

	@Column(name = "PASSWORD", nullable = false)
	private String password;

//	@Column(name = "STATUS")
//	private UserStatus status;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	private Set<Role> roles;

	public void setBusinessAddress(Address businessAddress) {
		this.businessAddress = businessAddress;
	}

	public void setBusinessTelephone(Telephone businessTelephone) {
		this.businessTelephone = businessTelephone;
	}

	public void setHiringDate(Calendar hiringDate) {
		this.hiringDate = hiringDate;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

//	public void setStatus(UserStatus status) {
//		this.status = status;
//	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Address getBusinessAddress() {
		return businessAddress;
	}

	public Telephone getBusinessTelephone() {
		return businessTelephone;
	}

	public Calendar getHiringDate() {
		return hiringDate;
	}
	
	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

//	public UserStatus getStatus() {
//		return status;
//	}

	public Set<Role> getRoles() {
		return roles;
	}

	@Override
	public int compareTo(User otherUser) {
		return username.compareToIgnoreCase(otherUser.username);
	}
}
